package me.yuzho.study.concurrency;


import static net.mindview.util.Print.*;

import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


/** 通过关闭底层资源来中断阻塞线程
 * @author yzhou-citrix
 *
 */
public class CloseResource {

    public static void main(String[] args) throws Exception {
        ExecutorService exec = Executors.newCachedThreadPool();
        
        ServerSocket server = new ServerSocket(8080);
        
        InputStream socketInput =  new Socket("localhost", 8080).getInputStream();
        
        exec.execute(new IOBlocked(socketInput));
        exec.execute(new IOBlocked(System.in));
        
        TimeUnit.MILLISECONDS.sleep(100);
        
        print("Shutting down all threads");
        
        exec.shutdownNow();
        
        TimeUnit.SECONDS.sleep(1);
        
        print("Closing " + socketInput.getClass().getName());
        
        socketInput.close(); // releases blocked thread
        
        TimeUnit.SECONDS.sleep(1);
        
        print("Closing  " + System.in.getClass().getName());
        
        System.in.close(); // releases blocked thread
        
        // interrupt() 发生在了关闭socket的时候，没有发生在关闭System.in时候
    }
}
